import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
return originalPush.call(this, location).catch(err => {})
};

Vue.use(VueRouter)

const routes = [
  
  {
    path:"/",
    redirect:"/home"
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children:[
      {
        path: 'searchpopup',
        component: () => import(/* webpackChunkName: "searchpopup" */ '../views/SearchPopup.vue')
      }
    ],
    meta:{
      tabbarNum:0,
      isShowTabbar:true
    }
  },
  // {
  //   path: '/searchpopup',
  //   name: 'SearchPopup',
  //   component: Category,
  //       component: () => import(/* webpackChunkName: "searchpopup" */ '../views/SearchPopup.vue')
  // },
  {
    path: '/topic',
    component: () => import(/* webpackChunkName: "topic" */ '../views/Topic.vue'),
    meta:{
      tabbarNum:1,
      isShowTabbar:true
    }
  },
  {
    path: '/category',
    component: () => import(/* webpackChunkName: "category" */ '../views/Category.vue'),
    meta:{
      tabbarNum:2,
      isShowTabbar:true
    }
  },
  {
    path: '/shopcart',
    component: () => import(/* webpackChunkName: "shopcart" */ '../views/ShopCart.vue'),
    meta:{
      tabbarNum:3,
      isShowTabbar:true
    }
  },
  {
    path: '/user',
    component: () => import(/* webpackChunkName: "category" */ '../views/User.vue'),
    meta:{
      tabbarNum:4,
      isShowTabbar:true
    }
  },
  {
    path: '/details',
    component: () => import(/* webpackChunkName: "details" */ '../views/Details.vue'),
    meta:{
      isShowTabbar:false
    }
  },
  // {
  //   path: '/about',
  //   name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫  能够在每次跳转路由前拦截下来,做一定的逻辑处理
// router.beforeEach((to,from,next)=>{
//   // from  路由从哪来  to  路由往哪去  next  必须写next才能跳转
//   if(to.path=="/shopcart"){
//     let token=localStorage.getItem("token")
//     // 点击购物车时判断有没有token,没有则说明没有登录
//     if(token){
//       next()
//     }else{
//       Vue.prototype.$toast("请先登录!")
//       setTimeout(() => {
//         next("/user")
//       }, 1000);
//     }
//     return    //结束这个函数
//   }
//   next()
// })

export default router
